import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
//要线程启动话，还得加上注解。这样才会执行到这个类
public class Task implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    public SimpMessagingTemplate wsTemplate;//=(SimpMessagingTemplate) SpringContextUtil.getBean("simpMessagingTemplate");

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent ) {
        System.out.println("application listener");
        //web有两个容器，onApplicationEvent会被调用两次，所以在root application context下执行逻辑
        if(contextRefreshedEvent.getApplicationContext().getParent() == null){//root application context 没有parent，
            WsMessageThread wsMessageThread = new WsMessageThread(wsTemplate.getMessageChannel(), wsTemplate.getMessageConverter());
            new Thread(wsMessageThread).start();
        }
    }
}